<?php
$this->pageTitle = Yii::app()->name . ' - Berita (' . $total . ')';

$this->breadcrumbs = array(
	'News',
);

$user = Yii::app()->user;
$this->menu = array(
	array(
		'label'=>'Pemeliharaan Berita',
		'url'=>array('news/admin'),
		'visible'=>$user->checkAccess('newsManage'),
	),
	array(
		'label'=>'Download Laporan (Ms Excel)',
		'url'=>array('download'),
		'visible'=>$user->checkAccess('newsDownload'),
	),
);
?>

<h1 id="title">Berita (<?php echo $total ?>)</h1>
<div class="byline" id="date">
	<?php
	echo Yii::app()->dateFormatter->formatDateTime($date, 'full', null)
	?>
</div>

<div id="list" class="view">
	<?php
	foreach ($dataProvider->getData() as $i=>$data) {
		$data = array(
			'data'=>$data,
			'rights'=>$rights,
			'index'=>$i,
			'dateFormat'=>null,
		);
		$this->renderPartial('_view', $data);
	}
	?>
</div>

<?php
$htmlOptions = array('class'=>'pager float-right', 'id'=>'next-day');
$tomorrow = $date + (24 * 60 * 60);
$params = array('date'=>$tomorrow);
$url = Yii::app()->urlManager->createUrl('news/index', $params);
echo CHtml::link('Hari Selanjutnya &gt;', $url, $htmlOptions);

$htmlOptions = array('class'=>'pager', 'id'=>'prev-day');
$yesterday = $date - (24 * 60 * 60);
$params = array('date'=>$yesterday);
$url = Yii::app()->urlManager->createUrl('news/index', $params);
echo CHtml::link('&lt; Hari Sebelumnya', $url, $htmlOptions);
?>

<?php $media = Yii::app()->request->baseUrl . "/medias/flashbeep_alert.swf"; ?>
<object id="beep-alert" class="flash" type="application/x-shockwave-flash"
		data="<?php echo $media ?>" width="1" height="1">
    <param name="movie" value="<?php echo $media ?>" />
</object>

<?php
ELoading::register();
$url = Yii::app()->urlManager->createUrl('news/index', $_GET);
?>

<script type="text/javascript">
	var _today = <?php echo time(); ?>;
	var _lastUpdate = <?php echo $lastUpdate ?>;
	var _date = <?php echo $date; ?>;
	var _months = {
		0: 'Januari',
		1: 'Februari',
		2: 'Maret',
		3: 'April',
		4: 'Mei',
		5: 'Juni',
		6: 'Juli',
		7: 'Agustus',
		8: 'September',
		9: 'Oktober',
		10: 'November',
		11: 'Desember'
	};

	function parseUrl(url) {
		var a =  document.createElement('a');
		a.href = url;
		return {
			source: url,
			protocol: a.protocol.replace(':',''),
			host: a.hostname,
			port: a.port,
			query: a.search,
			params: (function(){
				var ret = {},
				seg = a.search.replace(/^\?/,'').split('&'),
				len = seg.length, i = 0, s;
				for (;i<len;i++) {
					if (!seg[i]) { continue; }
					s = seg[i].split('=');
					ret[s[0]] = s[1];
				}
				return ret;
			})(),
			file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
			hash: a.hash.replace('#',''),
			path: a.pathname.replace(/^([^\/])/,'/$1'),
			relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
			segments: a.pathname.replace(/^\//,'').split('/')
		};
	}

	function check() {
		$.loading(false);
		$.loading(true, {onAjax:true, text: 'Membaharui berita...'});
		var data = {
			"date": _date,
			"lastUpdate": _lastUpdate,
			"ajax": 'news-index'
		};
		$.getJSON("<?php echo $url ?>", data, refresh);
	}

	function next() {
		$.loading(false);
		var tomorrow = _date + (24 * 60 * 60);
		var message = 'Memanggil berita hari selanjutnya...';
		$.loading(true, {onAjax:true, text: message});
		data = {
			"date": tomorrow,
			"lastUpdate": _lastUpdate,
			"ajax": 'news-index'
		};
		$.getJSON("<?php echo $url ?>", data, refresh);
	}

	function prev() {
		$.loading(false);
		var message = 'Memanggil berita hari sebelumnya...';
		$.loading(true, {onAjax:true, text: message});
		var yesterday = _date - (24 * 60 * 60);
		var data = {
			"date": yesterday,
			"lastUpdate": _lastUpdate,
			"ajax": 'news-index'
		};
		$.getJSON("<?php echo $url ?>", data, refresh);
	}

	function clickDay(day) {
		$.loading(false);
		var url = parseUrl(day.href);
		var year = url.params["year"];
		var month = url.params["month"] - 1;
		var day = url.params["day"];
		var dateName = day + " " + _months[month] + " " + year;
		var message = "Memanggil berita tanggal " + dateName + "...";
		$.loading(true, {onAjax:true, text: message});
		var date = new Date(year, month, day, 0, 0, 0, 0);
		var data = {
			"date": date.getTime() / 1000.0,
			"lastUpdate": _lastUpdate,
			"ajax": 'news-index'
		};
		$.getJSON("<?php echo $url ?>", data, refresh);
	}

	function refresh(json) {
		$.loading(false);
		date = parseFloat(json.date);
		lastUpdate = parseFloat(json.lastUpdate);
		if (lastUpdate != _lastUpdate || date != _date) {
			$('#date').html(json.dateHuman);
			$('#title').html('Berita (' + json.total + ')');
			$('#list').slideUp().html(json.values.join('')).slideDown();
			$.loading(true, {text: 'Berita terbaharui.', max: 3000});
			_lastUpdate = lastUpdate;
			_date = date;
			document.title = document.title.replace(/\d+/, json.total);
			document.getElementById('beep-alert').play(1, 1000);
		}
		else $.loading(true, {text: 'Tidak ada pembaharuan.', max: 3000});
	}
</script>

<?php
$script = "
setInterval(check, 60000);
$('#prev-day').click(function() {
	prev();
	return false;
});
$('#next-day').click(function() {
	next();
	return false;
});
$('#calendar a').click(function() {
	clickDay(this);
	return false;
});
";
Yii::app()->clientScript->registerScript('init', $script);
?>
